package com.github.ilioili.justdoit.components.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.config.EventConstants;
import com.github.ilioili.justdoit.model.dao.ScheduleEventDao;
import com.github.ilioili.justdoit.model.pojo.ScheduleEvent;
import com.github.ilioili.justdoit.util.AniUtil;
import com.github.ilioili.justdoit.util.CompatUtil;
import com.github.ilioili.justdoit.util.sql.SqlCallback;
import com.github.ilioili.justdoit.widget.CircleAnimationFrame;
import com.taihe.template.base.BaseActivity;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

import java.util.List;

@Layout(R.layout.activity_search)
public class SearchActivity extends BaseActivity {

    public static final int DURATION = 300;
    public static final int START_COLOR = Color.parseColor("#88000000");
    @Id(R.id.et_key_word)
    private EditText etKeyword;
    @Id(R.id.recyclerView)
    private RecyclerView recyclerView;
    @Id(R.id.circle_animation_frame)
    private CircleAnimationFrame circleAnimationFrame;
    private List<ScheduleEvent> dataList;

    private int fingerX;
    private int fingerY;
    RecyclerView.Adapter<Holder> adapter = new RecyclerView.Adapter<Holder>() {
        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = getLayoutInflater().inflate(R.layout.list_search_item, parent, false);
            return new Holder(v);
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            final ScheduleEvent scheduleEvent = dataList.get(position);
            holder.tvName.setText(scheduleEvent.name);

            if (scheduleEvent.isComplete) {
                holder.tvName.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                holder.tvName.setTextColor(CompatUtil.getColor(SearchActivity.this, R.color.font_gray));
            } else {
                holder.tvName.getPaint().setFlags(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                holder.tvName.setTextColor(scheduleEvent.color);
            }

            holder.tvName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (scheduleEvent.type == EventConstants.TYPE_EVENT) {
                        startActivity(CreateOrModifyActivity.newIntent(SearchActivity.this, scheduleEvent, BaseActivity.fingerUpX, BaseActivity.fingerUpY, scheduleEvent.color));
                    } else {
                        startActivity(DetailActivity.newIntent(SearchActivity.this, scheduleEvent, BaseActivity.fingerUpX, BaseActivity.fingerUpY));
                    }
                    hideSoftKeyboard();
                }
            });
        }

        @Override
        public int getItemCount() {
            return null == dataList ? 0 : dataList.size();
        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fingerX = BaseActivity.fingerUpX;
        fingerY = BaseActivity.fingerUpY;
        AniUtil.animateColor(Color.TRANSPARENT, START_COLOR, DURATION, getWindow().getDecorView());
        circleAnimationFrame.expand(true, fingerX, fingerY, DURATION, Color.TRANSPARENT);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        recyclerView.setAdapter(adapter);
        etKeyword.addTextChangedListener(new TextWatcher() {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(etKeyword.getText().toString().trim())) {
                        ScheduleEventDao.getAllMatchedEvents(etKeyword.getText().toString(), new SqlCallback<List<ScheduleEvent>>() {
                            @Override
                            public void onSucess(List<ScheduleEvent> scheduleEvents) {
                                dataList = scheduleEvents;
                                if (dataList.isEmpty()) {
                                    setVisiable(R.id.no_result);
                                } else {
                                    setGone(R.id.no_result);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        });
                    } else {
                        dataList = null;
                        setGone(R.id.no_result);
                        adapter.notifyDataSetChanged();
                    }
                }
            };

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                etKeyword.removeCallbacks(runnable);
                etKeyword.postDelayed(runnable, 100);
            }
        });
    }


    public void onClickBack(View v) {
        onBackPressed();
    }

    public void onClickClear(View v) {
        if(TextUtils.isEmpty(etKeyword.getText())){
            onBackPressed();
        }else{
            etKeyword.setText(null);
            dataList = null;
            adapter.notifyDataSetChanged();
        }
    }

    public static Intent newIntent(Context context) {
        return new Intent(context, SearchActivity.class);
    }

    @Override
    public void finish() {
        recyclerView.setAlpha(0);
        AniUtil.animateColor(START_COLOR, Color.TRANSPARENT, DURATION, getWindow().getDecorView());
        circleAnimationFrame.collapse(true, fingerX, fingerY, DURATION, true, new CircleAnimationFrame.CompleteListener() {
            @Override
            public void onComplete() {
                SearchActivity.super.finish();
                overridePendingTransition(0, 0);
            }
        });
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView tvName;

        public Holder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }
}
